package com.kcl.dfss.utils;

import java.util.ArrayList;
import java.util.List;

import com.kcl.dfss.R;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class UsageAdapter extends BaseAdapter {
	private Context mContext;
	private ArrayList<UsageRecord> mUsageList;
	private LayoutInflater mInflater = null;
	private Resources mRes = null;

	public UsageAdapter(Context context, ArrayList<UsageRecord> list) {
		mContext = context;
		mUsageList = new ArrayList<UsageRecord>(list);
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mRes = mContext.getResources();
	}
	public void setRecordsList(ArrayList<UsageRecord> list){
		mUsageList.clear();
		mUsageList.addAll(list);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mUsageList.size() != 0 ? mUsageList.size() : 1;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mUsageList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.layout_usagerecord, null);
			holder = new ViewHolder();
			holder.timeView = (TextView) convertView.findViewById(R.id.time);
			holder.sumeryView = (TextView) convertView
					.findViewById(R.id.summery);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (mUsageList.size() == 0) {
			holder.sumeryView.setText("没有使用记录");
		} else {
			UsageRecord record = mUsageList.get(position);
			holder.timeView.setText(String.format(
					mRes.getString(R.string.usage_record_time),
					record.mStartTime.hour, record.mStartTime.minute,
					record.mEndTime.hour, record.mEndTime.minute));
			holder.sumeryView.setText(String.format(
					mRes.getString(R.string.usage_record_summery),
					record.mDistance, record.mAveSpeed));
		}
		return convertView;
	}

	public static class ViewHolder {
		TextView timeView;
		TextView sumeryView;
	}

}
